package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.UserDao;
import com.aaa.entity.Card;
import com.aaa.entity.User;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;

public class UserDaoImpl implements UserDao {

    BaseDao baseDao = BaseDao.getInstance();

    @Override
    public int addUser(User user) {

        String sql = "insert into user(userId,userName,phone,status,idCard,birthday,sex,address,area," +
                "createdTime,cardId,momo,staffId) values " +
                "(?,?,?,?,?,?,?,?,?,?,?,?,?)";
        Object [] params = {user.getUserId(),user.getUserName(),user.getPhone(),user.getStatus(),user.getIdCard()
                ,user.getBirthday(),user.getSex(),user.getAddress(),user.getArea(),user.getCreatedTime(),user.getCardId(),
                user.getMomo(),user.getStaffId()};
        return baseDao.executeUpdate(sql,params) ;
    }

    @Override
    public int addCard(Card card) {
         String sql = "insert into card(cardId,userId,amount,credit,status,staffId,levelId) values " +
                "(?,?,?,?,?,?,?)";
        Object[] params = {card.getCardId(),card.getUserId(),card.getAmount(),card.getCredit(),card.getStatus()
                ,card.getStaffId(),card.getLevelId()};
        return baseDao.executeUpdate(sql,params);    
		}

    @Override
    public int updateUserByUserId(User user) {
String sql = "update user set userName=?,phone=?,status=?,idCard=?,address=? where cardId=?";
        Object[] params = {user.getUserName(),user.getPhone(),user.getStatus(),user.getIdCard(),user.getAddress(),user.getCardId()};

        return baseDao.executeUpdate(sql,params);

    }

    @Override
    public int deleteUserByuserId(User user) {
        String sql = "delete from user where userId = ?";
        Object[] objects = {user.getUserId()};
        return baseDao.executeUpdate(sql,objects);    
		}

    @Override
    public int deleteCardByuserId(Card card) {
        String sql = "delete from card where userId = ?";
        Object[] objects = {card.getUserId()};
        return baseDao.executeUpdate(sql,objects);
    }

    @Override
    public List<User> maxCardId() {
        String sql = "select max(cardId)+1 as cardId from user";
        return baseDao.query(sql,null,User.class);
    }

    @Override
    public List<User> getMaxUserId() {
        String sql = "select max(userId) as userId from user";
        return baseDao.query(sql,null,User.class);
    }

    @Override
    public List<Card> getMaxCardId() {
        String sql = "select max(cardId) as cardId from card";
        return baseDao.query(sql,null,Card.class);
    }

    @Override
    public List<Map<String, Object>> getDataByNearYear() {
        String sql = "select COUNT(*) amount,SUBSTRING(createdTime FROM 6 FOR 2) month from `user` GROUP BY SUBSTRING(createdTime FROM 6 FOR 2)";
        return baseDao.query(sql,null);
    }

    @Override//分页查询所有员工
    public List<User> getAllUser(Integer pageNumber, Integer pageSize, String searchId, String searchName) {
        /*user关联card*/
        String sql = "select u.*,c.amount  from user u,card c where c.userId = u.userId";
        if (StringUtils.isNotBlank(searchId)){
            sql += " and u.userId = "+searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and userName like'" + searchName + "'";
        }
        sql += " limit ?,?";
        pageNumber = (pageNumber-1)*pageSize;
        Object[] params = {pageNumber,pageSize};
        return baseDao.query(sql,params,User.class);
    
    }

    @Override
    public int getAllUserInfoCount(String searchId, String searchName) {
        String sql = "select count(1) len from user where 1 = 1";
        if (StringUtils.isNotBlank(searchId)) {
            sql += " and userId = " + searchId;
        }
        if (StringUtils.isNotBlank(searchName)) {
            searchName = "%" + searchName + "%";
            sql += " and userName like '" + searchName + "'";
        }
        List<Map<String, Object>> maps = baseDao.query(sql, null);
        if (maps != null && maps.size() > 0) {
            Map<String, Object> map = maps.get(0);
            Integer res = Integer.parseInt(map.get("len") + "");
            return res;
        }
        return 0;
    }


    /**
	*会员增长趋势统计
	*/
	@Override
    public int findUserCreatedTimeInfoForUserCount(String likeParam) {
        String sql = "select createdTime from `user` where createdTime like ";
        likeParam = "'%" + likeParam + "-%'";
        sql += likeParam;
        System.out.println(sql);
        Object[] params = {};
        List<String> list = baseDao.query(sql, params);
        return list.size();
    }

	/**
	*通过会员卡号进行会员信息查询
	*/
    @Override
    public List<String> findUserByCardid(Integer cardId) {
        String sql = "SELECT `user`.id,`user`.userId,`user`.userName,`user`.cardId,card.credit,card.amount,cardtype.`name` FROM cardtype,`user`,card " +
                "WHERE `user`.userId = card.userId AND card.levelId = cardtype.`level` and `card`.cardId=?";
        System.out.println(sql);
        Object[] params = {cardId};
        List<String> list = baseDao.query(sql, params);
        System.out.println(list);
        return list;
    }
}

